package com.mlming.springboot.consumer;

import com.google.gson.Gson;
import com.mlming.springboot.consts.MallConst;
import com.mlming.springboot.paysysclass.PayInfoVo;
import com.mlming.springboot.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付系统异步通知的RabbitMQ消费者:
 *      一旦消息队列有消息, 此处就会执行
 * 该消费者用于: 接收到异步消息, 获得到支付信息, 通过业务判断后, 把数据库的订单状态修改为 已支付
 */
@Component
@RabbitListener(queuesToDeclare = @Queue(value = MallConst.RABBITMQ_QUEUE_NAME,durable = "true"))
@Slf4j
public class PayMsgConsumer {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void setOrderStatus(String msg) {
        PayInfoVo payInfoVo = new Gson().fromJson(msg, PayInfoVo.class);
        log.info("异步通知接收,订单号为:"+payInfoVo.getOrderNo());
        // 验证该支付信息的状态: 如果支付成功,则去修改数据
        if("SUCCESS".equals(payInfoVo.getPlatformStatus())) {
            orderService.paidOrder(payInfoVo.getOrderNo());
            log.info("订单状态处理成功! 订单号为:"+payInfoVo.getOrderNo());
        }
    }

}
